<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>A页面</title>
</head>

<body>
  <p>我是A页面</p>
  <button id="btn_open">打开B页面</button>
  <button id="btn_send">发送数据</button>
  <button id="btn_iframe">iframe</button>
  <iframe id="otherWin" src="./2-B.html" width="200px" height="100px" frameborder="0"></iframe>
  <script>
    // 1.获取按钮dom节点
    const btnOpen = document.querySelector('#btn_open');
    const btnSend = document.querySelector('#btn_send');
    const btnIframe = document.querySelector('#btn_iframe');
    let win;
    // 2.绑定事件
    btnOpen.onclick = function () {
      // 通过新窗口打开B页面
      win = window.open('./2-B.html');
    }

    // 将win重新赋值
    btnIframe.onclick = function () {
      win = document.querySelector('#otherWin').contentWindow
    }

    btnSend.onclick = function () {
      // 调用方法传递数据
      win.postMessage('hello B.html')
    }

    // btnOpen.addEventListener('click',function() {

    // }, false)
  </script>
</body>

</html>